package com.starter.home.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.starter.home.domain.entity.ServeInfo;
import com.starter.home.domain.entity.ServeOrder;
import com.starter.home.dao.ServeOrderDao;
import com.starter.home.domain.vo.ServeOrderVO;
import com.starter.home.service.IServeImgService;
import com.starter.home.service.IServeInfoService;
import com.starter.home.service.IServeOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.starter.home.util.CommonParam;
import com.starter.home.util.PageUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author
 * @since 2020-05-01
 */
@Service
public class ServeOrderServiceImpl extends ServiceImpl<ServeOrderDao, ServeOrder> implements IServeOrderService {

    @Autowired
    private IServeInfoService infoService;
    @Autowired
    private IServeImgService imgService;

    @Override
    public PageUtil getList(Integer userId, CommonParam commonParam) {
        QueryWrapper<ServeOrder> queryWrapper = new QueryWrapper<>();
        List<ServeOrderVO> result = new ArrayList<>();
        List<ServeInfo> byUserId = infoService.getByUserId(userId);
        List<Integer> collect = byUserId.stream().map(ServeInfo::getId).collect(Collectors.toList());
        if (collect.size() > 0){
            queryWrapper.in("info_id",collect);
        }else{
            return new PageUtil(0,1,result);
        }
        //分页查询货物详情
        queryWrapper.ge("status",1);
        Page<ServeOrder> page = new Page<>(commonParam.getPage(),commonParam.getSize());
        queryWrapper.orderByAsc("id");
        Page<ServeOrder> pageData = this.baseMapper.selectPage(page,queryWrapper);
        List<ServeOrder> records = pageData.getRecords();

        ServeOrderVO serveOrderVO = null;
        for (ServeOrder serveOrder : records) {
            serveOrderVO = new ServeOrderVO();
            BeanUtils.copyProperties(serveOrder,serveOrderVO);
            serveOrderVO.setServeInfo(infoService.getBaseMapper().selectById(serveOrder.getInfoId()));
            result.add(serveOrderVO);
        }
        return new PageUtil(pageData.getTotal(),pageData.getCurrent(),result);
    }

    @Override
    public List<ServeOrderVO> converServeOrderVO(List<ServeOrder> serveOrders) {
        List<ServeOrderVO> list = new ArrayList<>();
        ServeOrderVO serveOrderVO = null;
        for (ServeOrder serveOrder : serveOrders) {
            serveOrderVO = new ServeOrderVO();
            BeanUtils.copyProperties(serveOrder,serveOrderVO);
            serveOrderVO.setServeInfo(infoService.getBaseMapper().selectById(serveOrder.getInfoId()));
            serveOrderVO.setImgList(imgService.selectByInfoId(serveOrder.getInfoId()));
            list.add(serveOrderVO);
        }
        return list;

    }

    @Override
    public int updateOrder(Integer integer, int i) {
        ServeOrder order = this.baseMapper.selectById(integer);
        order.setStatus(i);
        order.setCreateTime(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        int i1 = this.baseMapper.updateById(order);
        return i1;
    }
}
